package com.abc.robot01.activity

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color.Companion.White
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.lifecycleScope
import com.abc.robot01.R
import com.abc.robot01.activity.ShangPinKuActivity.Companion
import com.abc.robot01.retrofit.bean.Commodity
import com.abc.robot01.retrofit.bean.DesignPackage
import com.abc.robot01.vivemodel.XiangQingViewModel
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
import kotlinx.coroutines.launch

/**
 * 商品详情
 */
class XiangQingActivity : BaseVoiceActivity() {

    private val viewModel: XiangQingViewModel by viewModels()


    @Composable
    fun RowScope.Item(text: String, b: String, hasBorder: Boolean = false) {
        Box(
            contentAlignment = Alignment.CenterStart,
            modifier = Modifier
                .size(200.px, 84.px)
                .background(
                    color = MYBLACK_Percent5, // 设置背景颜色
                )

                .padding(start = 20.px, end = 0.px)
        ) {
            Text(
                text = text,
                style = TextStyle(
                    color = MYBLACK,
                    fontSize = 24.mysp
                )
            )
        }
        Box(
            contentAlignment = Alignment.CenterStart,
            modifier = Modifier
                .size(452.px, 84.px)
                .then(
                    if (hasBorder) {
                        Modifier.border(
                            width = 1.px,
                            color = MYBLACK_Percent10
                        )
                    } else Modifier
                )
                .background(
                    color = White, // 设置背景颜色
                )
                .padding(start = 20.px)
        ) {
            Text(
                text = b,
                style = TextStyle(
                    color = MYBLACK,
                    fontSize = 24.mysp
                )
            )
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel.toastMessage.observe(this) { message ->
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
        }

        val id = intent.getStringExtra("id")!!
        viewModel.load(id)
//        viewModel.loadProductDetail(id);
        setContent {
            val commodity by viewModel.commodity.collectAsState()

            val scrollState = rememberScrollState()
            MaterialTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
                    Row(
                        modifier = Modifier.padding(
                            start = 30.px,
                            end = 30.px,
                            top = 30.px
                        )
                    ) {
                        Spacer(modifier = Modifier.width(144.px))
                        Spacer(modifier = Modifier.width(82.px))

                        Column(
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(1400.px)
                        ) {
                            BasicText(
                                text = "详情",
                                style = TextStyle(
                                    fontSize = 30.mysp,
                                    color = Color.Black,
                                    fontWeight = FontWeight.Bold
                                ),
                            )
                            Spacer(modifier = Modifier.height(29.px))
                            MyCard {
                                Column(
                                    Modifier
                                        .padding(
                                            start = 30.px,
                                            end = 30.px,
                                            top = 30.px,
                                            bottom = 0.px
                                        )
                                        .verticalScroll(scrollState)
                                        .fillMaxSize(),
                                    verticalArrangement = Arrangement.spacedBy(30.px)
                                ) {
                                    commodity?.let {
                                        Content(it)
                                    }
                                }
                            }
                        }
                    }
                    Box(modifier = Modifier.padding(30.px)
                        .align(Alignment.BottomStart)) {
                        BackButton()
                    }
                }
            }
        }
        Log.e(TAG, "onCreate");
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ScrollableImage(url: State<String?>) {
        // Remember scroll states for both horizontal and vertical scrolling
//        val horizontalScrollState = rememberScrollState()
//        val verticalScrollState = rememberScrollState()
        Box(
            modifier = Modifier
                .size(540.px) // Set the viewport size to 200x200
                .background(Color.Transparent) // Optional background for the frame
//                .horizontalScroll(horizontalScrollState) // Enable horizontal scrolling
//                .verticalScroll(verticalScrollState) // Enable vertical scrolling
        ) {

            GlideImage(
                model = url.value,
                contentDescription = "Image",
                modifier = Modifier.background(Color.Transparent)
                    .size(540.px) // 限制宽高为540×540
                    .clickable {
                        // 点击事件
                    },
                contentScale = ContentScale.Fit // 或者使用其他的内容缩放策略，如 Fit、FillBounds 等
            )

//            GlideImage(
//                model = url.value,
//                contentDescription = "",
//                modifier = Modifier
//                    .clickable {
//                    },
//            )


            // Load and display the image with no scaling
//            Image(
//                painter = painterResource(id = R.drawable.x3),
//                contentDescription = "Scrollable Image",
////                modifier = Modifier.size(1000.dp), // The actual size of the image
//                contentScale = ContentScale.None // No scaling, show the image at its original size
//            )


        }
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ColumnScope.Content(commodity: Commodity) {
        val images = viewModel.images.collectAsState().value?.split(",")
        val big = viewModel.bigImg.collectAsState()
        val detailImg by viewModel.detailImg
        Row(horizontalArrangement = Arrangement.spacedBy(20.px)) {
            Column(verticalArrangement = Arrangement.spacedBy(20.px)) {
                images?.forEach { image ->
                    Log.e(TAG, "图片: $image")
                    GlideImage(
                        model = image,
                        contentScale = ContentScale.None , // 或者使用其他的内容缩放策略，如 Fit、FillBounds 等
                        contentDescription = "",
                        modifier = Modifier
                            .align(Alignment.CenterHorizontally)
                            .size(120.px, 120.px)
                            .clickable {
                                viewModel.bigImg.value = image
                            },
                    )

                }
            }
            ScrollableImage(big)
            Title()
        }
        Spacer(modifier = Modifier.height(40.px))
        Text(
            maxLines = 1,
            text = "参数",
            style = TextStyle(
                fontWeight = FontWeight.Bold,
                color = MYBLACK, fontSize = 30.mysp,
            )
        )
//        Spacer(modifier = Modifier.height(0.px))
        SubContent(commodity)
        //图片
        GlideImage(
            model = detailImg,
            contentDescription = "",
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
        )
    }

    @Composable
    fun SubContent(commodity: Commodity) {
        Log.e(TAG,"SubContent:"+commodity);
        val  companyName by viewModel.companyName
        Column {
            Row {
                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier
                        .size(200.px, 84.px)
                        .background(
                            color = MYBLACK_Percent5, // 设置背景颜色
                            shape = RoundedCornerShape(topStart = 20.px) // 设置圆角
                        )
                        .padding(start = 20.px, end = 84.px)
                ) {
                    Text(
                        text = "所属分类",
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }

                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier
                        .size(1104.px, 84.px)
                        .border(
                            width = 1.px,
                            color = MYBLACK_Percent10
                        )
                        .background(
                            color = White, // 设置背景颜色
                        )
                        .padding(start = 20.px)
                ) {
                    Text(
                        text = "" + commodity.firstCommodityTreeName + " - " + commodity.commodityTreeName,
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }
            }

            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .width(1304.px)
                    .height(84.px),
            ) {
                Row(
                    modifier = Modifier
                        .weight(1.0f)
                        .fillMaxHeight(),

                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    Item("类型", viewModel.categoryMap[commodity.typeStr] ?: "",hasBorder = true)
                }
                Row(
                    modifier = Modifier
                        .weight(1.0f)
                        .fillMaxHeight()
                        .background(Color.Transparent)
                    ,
                    verticalAlignment = Alignment.CenterVertically,
                ) {
//                    Item("商家", viewModel.categoryMap[commodity.typeStr] ?: "",hasBorder = true)
                    Item("商家", companyName,hasBorder = true)
                }
            }

            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .width(1304.px)
                    .height(84.px),
            ) {
                Row(
                    modifier = Modifier
                        .weight(1.0f)
                        .fillMaxHeight(),

                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    Item("施工损耗率", if(commodity.loss.toString()=="0.0") "/" else commodity.loss.toString())
                }
                Row(
                    modifier = Modifier
                        .weight(1.0f)
                        .fillMaxHeight()
                        .background(Color.Transparent)
                    ,
                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    Item("特殊系数", if(commodity.specialRatio == null || commodity.specialRatio == "null") "" else commodity.specialRatio.toString(),hasBorder = true)
                }
            }

//            Row {
//                Box(
//                    contentAlignment = Alignment.CenterStart,
//                    modifier = Modifier
//                        .size(200.px, 84.px)
//                        .background(
//                            color = MYBLACK_Percent5, // 设置背景颜色
//                            shape = RoundedCornerShape(topStart = 0.px) // 设置圆角
//                        )
//                        .padding(start = 20.px, end = 84.px)
//                ) {
//                    Text(
//                        text = "适用空间",
//                        style = TextStyle(
//                            color = MYBLACK,
//                            fontSize = 24.mysp
//                        )
//                    )
//                }
//
//                Box(
//                    contentAlignment = Alignment.CenterStart,
//                    modifier = Modifier
//                        .size(1104.px, 84.px)
//                        .border(
//                            width = 1.px,
//                            color = MYBLACK_Percent10
//                        )
//                        .background(
//                            color = White, // 设置背景颜色
//                        )
//                        .padding(start = 20.px)
//                ) {
//                    Text(
//                        text = viewModel.companyName2.value,
//                        style = TextStyle(
//                            color = MYBLACK,
//                            fontSize = 24.mysp
//                        )
//                    )
//                }
//            }

            Row {
                Box(
                    contentAlignment = Alignment.TopStart,
                    modifier = Modifier
                        .size(200.px, 200.px)
                        .background(
                            color = MYBLACK_Percent5, // 设置背景颜色
                            shape = RoundedCornerShape(topStart = 0.px) // 设置圆角
                        )
                        .padding(start = 20.px, end = 84.px, top = 30.px)
                ) {
                    Text(
                        text = "商品简介",
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }

                Box(
                    contentAlignment = Alignment.TopStart,
                    modifier = Modifier
                        .size(1104.px, 200.px)
                        .border(
                            width = 1.px,
                            color = MYBLACK_Percent10
                        )
                        .background(
                            color = White, // 设置背景颜色
                        )
                        .padding(start = 20.px, top = 30.px)
                ) {
                    Text(
                        text = commodity.comBody?:"",
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }
            }









//            Row {
//                Box(
//                    contentAlignment = Alignment.CenterStart,
//                    modifier = Modifier
//                        .size(200.px, 84.px)
//                        .background(
//                            color = MYBLACK_Percent5, // 设置背景颜色
//                        )
//                        .padding(start = 20.px, end = 84.px)
////                        .padding(16.dp) // 内边距，以确保文本不会紧贴边缘
//                ) {
//                    Text(
//                        text = "硬装标准",
//                        style = TextStyle(
//                            color = MYBLACK,
//                            fontSize = 24.mysp
//                        )
//                    )
//                }
//
//                Box(
//                    contentAlignment = Alignment.CenterStart,
//                    modifier = Modifier
//                        .size(1104.px, 84.px)
//                        .border(
//                            width = 1.px,
//                            color = MYBLACK_Percent10
//                        )
//                        .background(
//                            color = White, // 设置背景颜色
//                        )
//                        .padding(start = 20.px)
//                ) {
//                    Text(
//                        text = "" + commodity.name,
//                        style = TextStyle(
//                            color = MYBLACK,
//                            fontSize = 24.mysp
//                        )
//                    )
//                }
//            }


        }
    }

    @Composable
    fun RowScope.Title() {
        val commodity by viewModel.commodity.collectAsState()
        val brand by viewModel.brand.collectAsState()
        val series by viewModel.series.collectAsState()
        Column {
            Text(
                maxLines = 2,
                text = commodity?.name ?: "",
                style = TextStyle(
                    color = MYBLACK, fontSize = 30.mysp,
                )
            )

            Text(
                text = buildAnnotatedString {
                    withStyle(style = SpanStyle(fontSize = 24.mysp, color = MYRED)) {
                        append("¥")
                    }
                    withStyle(
                        style = SpanStyle(
                            fontSize = 42.mysp,
                            color = MYRED,
                            fontWeight = FontWeight.Bold
                        )
                    ) {
                        append(commodity?.price.toString() ?: "")
                    }
                    withStyle(style = SpanStyle(fontSize = 24.mysp, color = MYRED)) {
                        append("元/m²")
                    }
                }
            )

            Spacer(modifier = Modifier.height(15.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "品       牌",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                if (commodity != null) {
                    brand.forEach {
                        Text(
                            style = TextStyle(
                                color = MYBLACK, fontSize = 24.mysp,
                            ),
                            text = it.first,
                            modifier = Modifier
                                .background(
                                    color = MYBLACK_Percent5,
                                    shape = RoundedCornerShape(6.px)
                                ) // Set gray background
                                .padding(
                                    horizontal = 17.px,
                                    vertical = 10.px
                                ), // Set horizontal padding
                        )
                        Spacer(modifier = Modifier.width(10.px))
                    }
                }


            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "系       列",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                series.forEach {
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = it.first,
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
                }
            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "型       号",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                val a = commodity?.marking?.trim() ?: ""
                Log.e(TAG, "型       号:" + a);
                if (a.isNotEmpty()) {
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = a,
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
                }

            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "规       格",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )

                Spacer(modifier = Modifier.width(20.px))

                if (commodity != null) {
                    val labelNames = commodity!!.spec
                    labelNames?.split(",")?.forEach { name ->
                        Text(
                            style = TextStyle(
                                color = MYBLACK, fontSize = 24.mysp,
                            ),
                            text = name,
                            modifier = Modifier
                                .background(
                                    color = MYBLACK_Percent5,
                                    shape = RoundedCornerShape(6.px)
                                ) // Set gray background
                                .padding(
                                    horizontal = 17.px,
                                    vertical = 10.px
                                ), // Set horizontal padding
                        )
                        Spacer(modifier = Modifier.width(10.px))
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "onDestroy")
    }

    companion object {
        val TAG = "商品详情"
    }
}
